package com.tools.control;

import com.alibaba.fastjson.JSONObject;
import com.tools.alivideo.TestOpenAPI;
import com.tools.model.AliyunCreateJobModel;
import com.tools.model.AliyunListJobModel;
import com.tools.model.LayUiTableModel;
import com.tools.tools.HttpUtil;
import com.tools.tools.OSSAliUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by jianshan on 2018/1/21.
 */
@Controller
@RequestMapping("/aliyun")
public class AliyunController {


    private static String path="/home/admin/tmp/";


    public ExecutorService pool = Executors.newFixedThreadPool(100);

    private Logger logger = LoggerFactory.getLogger(AliyunController.class);



    @RequestMapping(value = "videoCreateJob.do")
    public String urlAudit(String token,Model model){

        return "videoCreateJob";
    }

    @RequestMapping(value = "videoListJob.do")
    public String videoListJob(String token,Model model){

        return "videoList";
    }

    @ResponseBody
    @RequestMapping(value = "listJob.do")
    public String videoListJob(String jobs){

        if(StringUtils.isBlank(jobs)){
            return "jobs参数为空";
        }

        try {
            String resp= HttpUtil.doGet(TestOpenAPI.listJobUrl(jobs),null);
            AliyunListJobModel model=JSONObject.parseObject(resp, AliyunListJobModel.class);
            List<AliyunListJobModel.AIJobListBean.AIJobBean> aiJobBeanList=model.getAIJobList().getAIJob();
            List result=new LinkedList();
            for(AliyunListJobModel.AIJobListBean.AIJobBean aiJob:aiJobBeanList){

                result.add(aiJob);
            }

            return JSONObject.toJSONString(result);

        }catch (Exception e){
            return null;
        }

    }


    @ResponseBody
    @RequestMapping(value="/upload",method= RequestMethod.POST)
    public LayUiTableModel upload(@RequestParam("file") MultipartFile file) throws Exception {


     try {
         if(!file.isEmpty()) {

             String filename = file.getOriginalFilename();
             File filepath = new File(path, filename);
             //判断路径是否存在，如果不存在就创建一个
             if (!filepath.getParentFile().exists()) {
                 filepath.getParentFile().mkdirs();
             }
             //将上传文件保存到一个目标文件当中

             String fileTmpName=System.currentTimeMillis()+file.getSize()+StringUtils.EMPTY;

             File tmp=new File(path,fileTmpName);
             file.transferTo(tmp);

             pool.submit(new UploadWorker(filename,tmp));


             return new LayUiTableModel(0, "sucess", 0, null);
         }
     }catch (Exception e){
         logger.error("upload has error",e);
     }
        return new LayUiTableModel(1, "error", 0, null);

    }


    class UploadWorker extends Thread{

        private File tmp;
        private String filename;

        public UploadWorker(String filename,File file) {
            super();
            this.tmp = file;
            this.filename=filename;
        }

        public void run(){
           String videoId = TestOpenAPI.testUploadURLStream(TestOpenAPI.access_key_id, TestOpenAPI.access_key_secret, filename, filename, OSSAliUtils.uploadFile("jianshan", filename, tmp));

            tmp.delete();

        }
    }




}
